<?php

function czy_setup() {

	// This theme uses wp_nav_menu() in one location.
	register_nav_menu( 'primary', '主菜单' );

	/*
	 * This theme uses a custom image size for featured images, displayed on
	 * "standard" posts and pages.
	 */
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 772, 329, true );

	add_image_size( 'post-thumbnail-200-130', 200, 130, true );//图片列表图片的大小
	add_image_size( 'post-thumbnail-236-133', 236, 133, true );//文章列表的图片大小
	add_image_size( 'post-thumbnail-70-70', 70, 70, true );
	add_image_size( 'post-thumbnail-315-219', 315, 249, true );
	add_image_size( 'post-thumbnail-291-264', 291, 264, true );

}
add_action( 'after_setup_theme', 'czy_setup' );

function get_new_category() {
	$categories = get_terms('cat-news', 'hide_empty=0' );//print_r($categories);

	return $categories;
}

function get_product_category() {
	$categories = get_terms('cat-product', 'hide_empty=0' );//print_r($categories);

	return $categories;
}

function get_the_new_tag( $id = false ) {
	$categories = get_the_terms( $id, 'tag-news' );
	if ( ! $categories || is_wp_error( $categories ) )
		$categories = array();

	$categories = array_values( $categories );

	foreach ( array_keys( $categories ) as $key ) {
		_make_cat_compat( $categories[$key] );
	}

	/**
	 * Filter the array of categories to return for a post.
	 *
	 * @since 3.1.0
	 *
	 * @param array $categories An array of categories to return for the post.
	 */
	return apply_filters( 'get_the_new_tag', $categories );
}

function the_new_tag( $post_id = false)
{
	global $post;

	if( !$post_id )
		$post_id = $post->ID;

	$tags = get_the_new_tag($post_id);
	
	$str = '';
	foreach($tags as $tag) {
		$str .= "{$tag->name},";
	}

	echo substr($str, 0, -1);
}
//文章浏览次数
function get_post_views ($post_id) {
    $count_key = 'post_views_count';
    $count = get_post_meta($post_id, $count_key, true);
    if ($count == '') {
        delete_post_meta($post_id, $count_key);
        add_post_meta($post_id, $count_key, '0');
        $count = '0';
    }
    echo number_format_i18n($count);
}

//页码/分页/翻页效果
function wpdx_paging_nav(){
	global $wp_query;
 
	$big = 999999999; // 需要一个不太可能的整数
 
	$pagination_links = paginate_links( array(
		'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
		'format' => '?paged=%#%',
		'current' => max( 1, get_query_var('paged') ),
		'total' => $wp_query->max_num_pages
	) );
 
echo $pagination_links;
}

//限制文章列表的字数的
function custom_excerpt_length( $length ) {
    return 100;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );


/**
 * 获取最热文章
 *
 * @since 3.0.0
 * @return array [最热文章数组]
 */
function zan_get_hotest_posts($num) {
	$args = array(
		'posts_per_page'   => $num,
		'offset'           => 0,
		'category'         => '',
		'orderby'          => 'comment_count',
		'order'            => 'DESC',
		'include'          => '',
		'exclude'          => '',
		'meta_key'         => '',
		'meta_value'       => '',
		'post_type'        => 'post',
		'post_mime_type'   => '',
		'post_parent'      => '',
		'post_status'      => 'publish',
		'suppress_filters' => true
	);

	return get_posts($args);
}

/**
 * 获取最新文章
 *
 * @since 3.0.0
 * @return array [最新文章数组]
 */
function zan_get_latest_posts($num) {
  $args = array(
    'posts_per_page'   => $num,
    'offset'           => 0,
    'category'         => '',
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'include'          => '',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => '',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'post_status'      => 'publish',
    'suppress_filters' => true
  );

  return get_posts($args);
}

/**
 * 获取随机文章
 *
 * @since 3.0.2
 * @return array [最新文章数组]
 */
function zan_get_rand_posts($num) {
  $args = array(
    'posts_per_page'   => $num,
    'offset'           => 0,
    'category'         => '',
    'orderby'          => 'rand',
    'order'            => 'DESC',
    'include'          => '',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => '',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'post_status'      => 'publish',
    'suppress_filters' => true
  );

  return get_posts($args);
}


//分页  
function par_pagenavi($range = 10){  
    global $paged, $wp_query;  
    if ( !$max_page ) {$max_page = $wp_query->max_num_pages;}  
    if($max_page > 1){if(!$paged){$paged = 1;}  
        if($paged != 1){echo "<a href='" . get_pagenum_link(1) . "' class='extend' title='跳转到首页'> 返回首页 </a>";}  
        previous_posts_link(' 上一页 ');  
        if($max_page > $range){  
            if($paged < $range){for($i = 1; $i <= ($range + 1); $i++){echo "<a href='" . get_pagenum_link($i) ."'";  
                if($i==$paged)echo " class='current'";echo ">$i</a>";}}  
            elseif($paged >= ($max_page - ceil(($range/2)))){  
                for($i = $max_page - $range; $i <= $max_page; $i++){echo "<a href='" . get_pagenum_link($i) ."'";  
                    if($i==$paged)echo " class='current'";echo ">$i</a>";}}  
            elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){  
                for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){echo "<a href='" . get_pagenum_link  
  
($i) ."'";if($i==$paged) echo " class='current'";echo ">$i</a>";}}}  
        else{for($i = 1; $i <= $max_page; $i++){echo "<a href='" . get_pagenum_link($i) ."'";  
            if($i==$paged)echo " class='current'";echo ">$i</a>";}}  
        next_posts_link(' 下一页 ');  
        if($paged != $max_page){echo "<a href='" . get_pagenum_link($max_page) . "' class='extend' title='跳转到最后一页'> 最后一页   
  
</a>";}}  
}  




